<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Running with worker threads</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Fiber">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Fiber">
<link rel="prev" href="gpu_computing/hip.html" title="ROCm/HIP">
<link rel="next" href="performance.html" title="Performance">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="gpu_computing/hip.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="performance.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="fiber.worker"></a><a name="worker"></a><a class="link" href="worker.html" title="Running with worker threads">Running with worker
    threads</a>
</h2></div></div></div>
<h4>
<a name="fiber.worker.h0"></a>
      <span><a name="fiber.worker.keep_workers_running"></a></span><a class="link" href="worker.html#fiber.worker.keep_workers_running">Keep
      workers running</a>
    </h4>
<p>
      If a worker thread is used but no fiber is created or parts of the framework
      (like <a class="link" href="fiber_mgmt/this_fiber.html#this_fiber_yield"><code class="computeroutput">this_fiber::yield()</code></a>) are touched, then no fiber scheduler
      is instantiated.
    </p>
<pre class="programlisting"><span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span>
        <span class="special">[]{</span>
            <span class="comment">// fiber scheduler not instantiated</span>
        <span class="special">});</span>
<span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span>
</pre>
<p>
      If <span class="emphasis"><em>use_scheduling_algorithm&lt;&gt;()</em></span> is invoked, the
      fiber scheduler is created. If the worker thread simply returns, destroys the
      scheduler and terminates.
    </p>
<pre class="programlisting"><span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span>
        <span class="special">[]{</span>
            <span class="comment">// fiber scheduler created</span>
            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span><span class="identifier">my_fiber_scheduler</span><span class="special">&gt;();</span>
        <span class="special">});</span>
<span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span>
</pre>
<p>
      In order to keep the worker thread running, the fiber associated with the thread
      stack (which is called <span class="quote">&#8220;<span class="quote">main</span>&#8221;</span> fiber) is blocked. For instance
      the <span class="quote">&#8220;<span class="quote">main</span>&#8221;</span> fiber might wait on a <a class="link" href="synchronization/conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a>.
      For a gracefully shutdown <a class="link" href="synchronization/conditions.html#class_condition_variable"><code class="computeroutput">condition_variable</code></a> is signalled
      and the <span class="quote">&#8220;<span class="quote">main</span>&#8221;</span> fiber returns. The scheduler gets destructed if
      all fibers of the worker thread have been terminated.
    </p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="identifier">mtx</span><span class="special">;</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">condition_variable_any</span> <span class="identifier">cv</span><span class="special">;</span>
<span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span>
        <span class="special">[&amp;</span><span class="identifier">mtx</span><span class="special">,&amp;</span><span class="identifier">cv</span><span class="special">]{</span>
            <span class="identifier">mtx</span><span class="special">.</span><span class="identifier">lock</span><span class="special">();</span>
            <span class="comment">// suspend till signalled</span>
            <span class="identifier">cv</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span><span class="identifier">mtx</span><span class="special">);</span>
            <span class="identifier">mtx</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">();</span>
        <span class="special">});</span>
<span class="comment">// signal termination</span>
<span class="identifier">cv</span><span class="special">.</span><span class="identifier">notify_all</span><span class="special">();</span>
<span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span>
</pre>
<h4>
<a name="fiber.worker.h1"></a>
      <span><a name="fiber.worker.processing_tasks"></a></span><a class="link" href="worker.html#fiber.worker.processing_tasks">Processing
      tasks</a>
    </h4>
<p>
      Tasks can be transferred via channels. The worker thread runs a pool of fibers
      that dequeue and executed tasks from the channel. The termination is signalled
      via closing the channel.
    </p>
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">task</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">function</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">()&gt;;</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">buffered_channel</span><span class="special">&lt;</span><span class="identifier">task</span><span class="special">&gt;</span> <span class="identifier">ch</span><span class="special">{</span><span class="number">1024</span><span class="special">};</span>
<span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span>
        <span class="special">[&amp;</span><span class="identifier">ch</span><span class="special">]{</span>
            <span class="comment">// create pool of fibers</span>
            <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">=</span><span class="number">0</span><span class="special">;</span> <span class="identifier">i</span><span class="special">&lt;</span><span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span><span class="special">{</span>
                    <span class="special">[&amp;</span><span class="identifier">ch</span><span class="special">]{</span>
                        <span class="identifier">task</span> <span class="identifier">tsk</span><span class="special">;</span>
                        <span class="comment">// dequeue and process tasks</span>
                        <span class="keyword">while</span> <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">closed</span><span class="special">!=</span><span class="identifier">ch</span><span class="special">.</span><span class="identifier">pop</span><span class="special">(</span><span class="identifier">tsk</span><span class="special">)){</span>
                            <span class="identifier">tsk</span><span class="special">();</span>
                        <span class="special">}</span>
                    <span class="special">}}.</span><span class="identifier">detach</span><span class="special">();</span>
            <span class="special">}</span>
            <span class="identifier">task</span> <span class="identifier">tsk</span><span class="special">;</span>
            <span class="comment">// dequeue and process tasks</span>
            <span class="keyword">while</span> <span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">channel_op_status</span><span class="special">::</span><span class="identifier">closed</span><span class="special">!=</span><span class="identifier">ch</span><span class="special">.</span><span class="identifier">pop</span><span class="special">(</span><span class="identifier">tsk</span><span class="special">)){</span>
                <span class="identifier">tsk</span><span class="special">();</span>
            <span class="special">}</span>
        <span class="special">});</span>
<span class="comment">// feed channel with tasks</span>
<span class="identifier">ch</span><span class="special">.</span><span class="identifier">push</span><span class="special">([]{</span> <span class="special">...</span> <span class="special">});</span>
<span class="special">...</span>
<span class="comment">// signal termination</span>
<span class="identifier">ch</span><span class="special">.</span><span class="identifier">close</span><span class="special">();</span>
<span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span>
</pre>
<p>
      An alternative is to use a work-stealing scheduler. This kind of scheduling
      algorithm a worker thread steals fibers from the ready-queue of other worker
      threads if its own ready-queue is empty.
    </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
        Wait till all worker threads have registered the work-stealing scheduling
        algorithm.
      </p></td></tr>
</table></div>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">mutex</span> <span class="identifier">mtx</span><span class="special">;</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">condition_variable_any</span> <span class="identifier">cv</span><span class="special">;</span>
<span class="comment">// start wotrker-thread first</span>
<span class="keyword">auto</span> <span class="identifier">worker</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">thread</span><span class="special">(</span>
        <span class="special">[&amp;</span><span class="identifier">mtx</span><span class="special">,&amp;</span><span class="identifier">cv</span><span class="special">]{</span>
            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">work_stealing</span><span class="special">&gt;(</span><span class="number">2</span><span class="special">);</span>
            <span class="identifier">mtx</span><span class="special">.</span><span class="identifier">lock</span><span class="special">();</span>
            <span class="comment">// suspend main-fiber from the worker thread</span>
            <span class="identifier">cv</span><span class="special">.</span><span class="identifier">wait</span><span class="special">(</span><span class="identifier">mtx</span><span class="special">);</span>
            <span class="identifier">mtx</span><span class="special">.</span><span class="identifier">unlock</span><span class="special">();</span>
        <span class="special">});</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">use_scheduling_algorithm</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">algo</span><span class="special">::</span><span class="identifier">work_stealing</span><span class="special">&gt;(</span><span class="number">2</span><span class="special">);</span>
<span class="comment">// create fibers with tasks</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">fibers</span><span class="special">::</span><span class="identifier">fiber</span> <span class="identifier">f</span><span class="special">{[]{</span> <span class="special">...</span> <span class="special">}};</span>
<span class="special">...</span>
<span class="comment">// signal termination</span>
<span class="identifier">cv</span><span class="special">.</span><span class="identifier">notify_all</span><span class="special">();</span>
<span class="identifier">worker</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span>
</pre>
<div class="important"><table border="0" summary="Important">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../doc/src/images/important.png"></td>
<th align="left">Important</th>
</tr>
<tr><td align="left" valign="top"><p>
        Because the TIB (thread information block on Windows) is not fully described
        in the MSDN, it might be possible that not all required TIB-parts are swapped.
        Using WinFiber implementation might be an alternative (see documentation
        about <a href="http://www.boost.org/doc/libs/1_65_1/libs/context/doc/html/context/cc/implementations__fcontext_t__ucontext_t_and_winfiber.html" target="_top"><span class="emphasis"><em>implementations
        fcontext_t, ucontext_t and WinFiber of boost.context</em></span></a>).
      </p></td></tr>
</table></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2013 Oliver Kowalke<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="gpu_computing/hip.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="performance.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
